package com.hotline.communication.controller;


import com.hotline.communication.constant.Constants;
import com.hotline.communication.constants.DateConstant;
import com.hotline.communication.dto.*;
import com.hotline.communication.enums.*;
import com.hotline.communication.service.*;
import com.hotline.communication.util.*;

import java.util.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static  com.hotline.communication.util.StringUtils.null2String;
import static  com.hotline.communication.util.StringUtils.string2Int;

@Controller
@RequestMapping("/comm")
@Slf4j
public class CommunicationVxmlController {

    @Autowired
    private WorkTimeService workTimeService;

    @Autowired
    private HolidayService holidayService;

    @Autowired
    private CallOperationDetailService callOperationDetailService;

    @Autowired
    private CallLogService callLogService;

    @Autowired
    private RecordLogService recordLogService;

    @Autowired
    private OrderSeqService orderSeqService;

    @Autowired
    private SatisfactionService satisfactionService;

    @Autowired
    private DutyUserService dutyUserService;

    @Autowired
    private DutyUserCallService dutyUserCallService;

    @Autowired
    private ParamSetService paramSetService;

    @Autowired
    private BlackInfoService blackInfoService;

    @Autowired
    private IvrService ivrService;

    @InitBinder("communicationDTO")
    public void initBinder(WebDataBinder webDataBinder) {
        webDataBinder.setFieldDefaultPrefix("session.sce.");
    }

    /**
     * 判断是否工作时间
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/workTime/vxml")
    public String workTime(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){

        log.info("工作时间参数请求参数:{}",communicationDTO);

        //默认是工作时间
        Map<String,String> result = new LinkedHashMap<>();
        result.put("rs","1");

        String unitCode = UnitCodeUtil.getUnitCode(communicationDTO.getSUnitCode());

        //查询是否节假日
        String today = DateUtils.format(new Date(),"yyyy-M-d");
        Integer holidayCount = holidayService.count(unitCode,today);
        //是节假日，返回非工作时间
        if(holidayCount != null && holidayCount >= 1){
            result.put("rs","0");
            String vxml = VxmlUtil.createVxml(result);
            log.info("判断是否工作时间结果返回:{}",vxml);
            return vxml;
        }

        //查询是否工作时间
        Integer workTimeCount = workTimeService.count(unitCode);
        //非工作时间
        if(workTimeCount == null || workTimeCount == 0){
            result.put("rs","0");
            String vxml = VxmlUtil.createVxml(result);
            log.info("判断是否工作时间结果返回:{}",vxml);
            return vxml;
        }

        String vxml = VxmlUtil.createVxml(result);

        log.info("判断是否工作时间结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 获取来电号码的用户类型
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkPhone/vxml")
    public String checkPhone(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){

        log.info("获取来电号码的用户类型请求参数:{}",communicationDTO);

        //用户类别，0：普通，1：黑名单，2：vip,默认为普通用户
        Map<String,String> result = new LinkedHashMap<>();
        result.put("rs","0");

        String callerNo = null2String(communicationDTO.getSCallerNo());
        String unitCode = null2String(communicationDTO.getSUnitCode());
        BlackInfoDTO blackInfoDTO = blackInfoService.selectByPhoneNumberAndOrgId(callerNo, unitCode);
        if(Objects.nonNull(blackInfoDTO)){
            CallerLevelEnum callerLevelEnum = CallerLevelEnum.getCallerLevelEnum(blackInfoDTO.getCallerLevel());
            if(Objects.nonNull(callerLevelEnum)){
                switch (callerLevelEnum){
                    case BLACK:
                        result.put("rs","1");
                        break;
                    case WHITE:
                        result.put("rs","2");
                        break;
                    case SPECIAL:
                        result.put("rs","4");
                        break;
                }
            }
        }

        String vxml = VxmlUtil.createVxml(result);

        log.info("获取来电号码的用户类型结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 解析随路数据
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/callData/vxml")
    public String callData(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){

        log.info("解析随路数据请求参数:{}",communicationDTO);

        String callData = communicationDTO.getSData();

        //默认是工作时间
        Map<String,String> result = new LinkedHashMap<>();
        result.put("rs","");
        result.put("rsB","");
        result.put("rsC","");
        result.put("rsD","");
        result.put("rsE","");
        result.put("rsF","");
        result.put("rsG","");

        if(StringUtils.isBlank(callData)){
            String vxml = VxmlUtil.createVxml(result);
            log.info("解析随路数据结果返回:{}",vxml);
            return vxml;
        }

        String callDataArr[] = callData.trim().split("&");

        for( int i = callDataArr.length ;i > 0; i-- ){
            switch (i){
                case 1:{
                    result.put("rs", callDataArr[0]);
                    break;
                }
                case 2:{
                    result.put("rsB", callDataArr[1]);
                    break;
                }
                case 3:{
                    result.put("rsC", callDataArr[2]);
                    break;
                }
                case 4:{
                    result.put("rsD", callDataArr[3]);
                    break;
                }
                case 5:{
                    result.put("rsE", callDataArr[4]);
                    break;
                }
                case 6:{
                    result.put("rsF", callDataArr[5]);
                    break;
                }
                case 7:{
                    result.put("rsG", callDataArr[6]);
                    break;
                }
            }
        }

        String vxml = VxmlUtil.createVxml(result);
        log.info("解析随路数据结果返回:{}",vxml);
        return vxml;
    }

    /**
     * ivr日志请求
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/ivr/vxml")
    public String ivr(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){
        log.info("ivr日志请求请求参数:{}",communicationDTO);

        CallOperationDetailDTO callOperationDetailDTO = new CallOperationDetailDTO();
        callOperationDetailDTO.setCallId(communicationDTO.getSCallId());
        callOperationDetailDTO.setUnitCode(UnitCodeUtil.getUnitCode(communicationDTO.getSUnitCode()));

        String track = communicationDTO.getSTrack();
        if(StringUtils.isNotBlank(track)){
            track = org.springframework.util.StringUtils.replace(track,"*","");
            track = track.trim();
        }
        String id = UUIDUtils.getUUID();
        callOperationDetailDTO.setId(id);
        callOperationDetailDTO.setOperationNo(track);

        callOperationDetailService.insert(callOperationDetailDTO);

        //查询按键信息
        IvrDTO ivrDTO  = new IvrDTO();
        ivrDTO.setOrgId(null2String(communicationDTO.getSUnitCode()));
        ivrDTO.setKeyValue(track);
        IvrDTO selectOneResult = ivrService.selectOne(ivrDTO);
        //更新call_log表的按键信息
        if (Objects.nonNull(selectOneResult)) {
            CallLogDTO callLogDTO = new CallLogDTO();
            callLogDTO.setCallId(communicationDTO.getSCallId());
            callLogDTO.setKeyValue(track);
            callLogDTO.setIvrName(selectOneResult.getName());
            callLogService.update(callLogDTO);
        }

        String vxml = VxmlUtil.createVxml();

        log.info("ivr日志请求结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 获取值班电话
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/tel/vxml")
    public String tel(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){
        log.info("获取值班电话请求参数:{}",communicationDTO);

        String unitCode = communicationDTO.getSUnitCode();

        String transferType = paramSetService.getParamValue(ParamEnum.TX_TRANSFER_TYPE.getCode(), unitCode);
        if(Objects.isNull(transferType)){
            transferType = "1";
        }
        String callId = communicationDTO.getSCallId();
        DutyUserDTO dutyUserDTO = dutyUserService.getDutyUser(unitCode,callId,null);
        String phone = "";
        if(Objects.nonNull(dutyUserDTO)){
            //出局号码
            String callOutPrefix = paramSetService.getParamValue(ParamEnum.TX_CALL_OUT_PREFIX.getCode(), unitCode);
            phone = callOutPrefix + dutyUserDTO.getPhone();
        }
        Map<String,String> result = new LinkedHashMap<>();
        result.put("rs",phone);
        result.put("rsB",transferType);

        String vxml = VxmlUtil.createVxml(result);

        log.info("获取值班电话结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 录音日志
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/rec/vxml")
    public String record(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){
        log.info("录音日志请求参数:{}",communicationDTO);

        String recordId = null2String(communicationDTO.getSXh());

        String callId = null2String(communicationDTO.getSCallId());

        String unitCode = null2String(communicationDTO.getSUnitCode());
        unitCode = UnitCodeUtil.getUnitCode(unitCode);

        String recordType = null2String(communicationDTO.getSRecType());

        String caller = null2String(communicationDTO.getSCallerNo());

        String called = null2String(communicationDTO.getSCalledNo());

        String recordFileName = null2String(communicationDTO.getSRecFileName());

        String operationTypeStr = null2String(communicationDTO.getSPlayType());

        String callType = null2String(communicationDTO.getSCallType());

        String recordFilePath = null2String(communicationDTO.getSRecFilePath());

        String recordBusinessCodeStr = null2String(communicationDTO.getSRecBusiCode());
        Integer recordBusinessCode = string2Int(recordBusinessCodeStr);

        String appId = null2String(communicationDTO.getAppId());

        Integer operationType = string2Int(operationTypeStr);
        RecordOperationTypeEnum recordOperationTypeEnum = RecordOperationTypeEnum.getRecordOperationTypeEnum(operationType);

        RecordLogDTO recordLogDTO = new RecordLogDTO();
        recordLogDTO.setUnitCode(unitCode);
        recordLogDTO.setCallId(callId);

        switch (recordOperationTypeEnum){
            //开始录音
            case START_RECORD:{
                //生成ID
                recordId = getRecordId();
                recordLogDTO.setId(recordId);
                //录音文件
                recordFileName = recordId + Constants.RECORD_EXT;
                recordLogDTO.setRecordFileName(recordFileName);

                Integer businessTypeCode = getBusinessTypeCode(recordBusinessCode);
                recordLogDTO.setRecordBusinessCode(businessTypeCode);

                recordLogDTO.setCallType(string2Int(callType));
                recordLogDTO.setCaller(caller);
                recordLogDTO.setRecordType(string2Int(recordType));

                //呼转录音,设置录音接听人信息
                if(RecordTypeEnum.TRANSFER_RECORD.getCode().equals(string2Int(recordType))){
                    DutyUserCallDTO dutyUserCallDTO = dutyUserCallService.selectByCallId(callId);
                    if(Objects.nonNull(dutyUserCallDTO)){
                        recordLogDTO.setUserId(dutyUserCallDTO.getUserId());
                        recordLogDTO.setUserName(dutyUserCallDTO.getUserName());
                        recordLogDTO.setWorkNumber(dutyUserCallDTO.getWorkNumber());

                        CallLogDTO callLogDTO = new CallLogDTO();
                        callLogDTO.setCallId(callId);
                        callLogDTO.setInQueueTime(new Date());
                        callLogService.update(callLogDTO);
                    }
                }

                recordLogService.insert(recordLogDTO);

                break;
            }
            //重新录音
            case RERECORD:{
                recordLogDTO.setId(recordId);
                recordLogDTO.setRecordBeginTime(new Date());
                recordLogService.update(recordLogDTO);
                //呼转录音
                if(RecordTypeEnum.TRANSFER_RECORD.getCode().equals(string2Int(recordType))){
                    updateCallLog(callId);
                }
                break;
            }
            //结束录音
            case END_RECORD:{
                recordLogDTO.setId(recordId);
                recordLogDTO.setRecordEndTime(new Date());
                recordLogDTO.setRecordFileName(recordFileName);
                recordLogService.update(recordLogDTO);
                //呼转录音
                if(RecordTypeEnum.TRANSFER_RECORD.getCode().equals(string2Int(recordType))){
                    DutyUserCallDTO dutyUserCallDTO = dutyUserCallService.selectByCallId(callId);
                    if(Objects.nonNull(dutyUserCallDTO)){
                        RecordLogDTO recordLog = recordLogService.selectById(recordId);
                        if(Objects.nonNull(recordLog)){
                            //更新call_log表记录来电接听坐席信息
                            updateCallLog(dutyUserCallDTO,callId,recordLog.getRecordBeginTime());
                        }

                    }
                }
                //自动留言
                else if(RecordTypeEnum.AUTO_RECORD.getCode().equals(string2Int(recordType))){
                    updateCallLog(callId,unitCode,caller);
                }
                break;
            }
            //外呼失败
            case CALL_FAILED:{
                //外呼失败，录音也就失败了，将录音记录删除
                recordLogService.delete(recordId);
                break;
            }
        }

        if(StringUtils.isNotBlank(recordFileName)){
            recordFileName = recordFileName.replaceAll("\\\\", "_");
        }

        Map<String,String> result = new LinkedHashMap<>();
        result.put("sCallid", callId);
        result.put("sCallerNo", caller);
        result.put("inCallType", called);
        result.put("inRecType", recordType);
        result.put("inRecBusiCode", recordBusinessCodeStr);
        result.put("inOperType", operationTypeStr);
        result.put("sUnitCode", unitCode);
        result.put("rs", recordFileName);
        result.put("rsB", "");
        result.put("rsC", recordId);

        String vxml = VxmlUtil.createVxml(result);

        log.info("录音日志结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 转外线成功接听后，记录接听坐席信息
     *
     * @param dutyUserCallDTO 值班人员呼叫数据
     * @param callId 呼叫流水号
     * @param recordBeginTime 开始录音时间，开始录音时间为坐席接通时间
     */
    private void updateCallLog(DutyUserCallDTO dutyUserCallDTO,String callId,Date recordBeginTime){
        CallLogDTO callLogDTO = new CallLogDTO();
        callLogDTO.setCallId(callId);
        callLogDTO.setUserId(dutyUserCallDTO.getUserId());
        callLogDTO.setUserName(dutyUserCallDTO.getUserName());
        callLogDTO.setWorkNumber(dutyUserCallDTO.getWorkNumber());
        callLogDTO.setInSeatUnitCode(dutyUserCallDTO.getUserOrgId());
        callLogDTO.setRingTime(recordBeginTime);
        callLogDTO.setInSeatTime(recordBeginTime);

        callLogService.update2(callLogDTO);
    }

    /**
     * 结束录音更新call_log挂机时间
     *
     * @param callId
     */
    private void updateCallLog(String callId){
        CallLogDTO callLogDTO = new CallLogDTO();
        callLogDTO.setCallId(callId);
        callLogDTO.setOutTime(new Date());
        callLogService.update(callLogDTO);
    }

    /**
     * 自动留言结束录音，是否计入呼损更新call_log
     *
     * @param callId
     */
    private void updateCallLog(String callId,String unitCode,String caller){

        //来电录音留言是否计入呼损记录
        String sCallingRecordIncludedCallLoss = paramSetService.getParamValue(ParamEnum.TX_CALLING_RECORD_INCLUDED_CALL_LOSS.getCode(), unitCode);
        Integer callingRecordIncludedCallLoss = string2Int(sCallingRecordIncludedCallLoss);
        //来电留言不计入呼损,且当前时间为坐席上班时间，更新坐席时间振铃时间和坐席接听时间
        if(WhetherEnum.NO.getCode().equals(callingRecordIncludedCallLoss)){

            //查询是否节假日
            String today = DateUtils.format(new Date(),"yyyy-M-d");
            Integer holidayCount = holidayService.count(unitCode,today);

            //查询是否工作时间
            Integer workTimeCount = workTimeService.count(unitCode);

            //坐席工作时间
            if(holidayCount <= 0 && workTimeCount >= 1){

                CallLogDTO callLogDTO = new CallLogDTO();
                callLogDTO.setCallId(callId);

                callLogDTO.setOutTime(new Date());

                Date now = new Date();
                callLogDTO.setRingTime(now);
                callLogDTO.setInSeatTime(now);

                //录音留言是否计入呼损标记
                callLogDTO.setIsIncludedCallLoss(WhetherEnum.NO.getCode());

                // 查询当天来电记录是否有呼损记录，如果有，将当天来电标记为呼损的记录，
                // 自动去掉呼损标记，标识为接听
                CallLogDTO param = new CallLogDTO();
                param.setStartTime(today);
                param.setEndTime(today);
                param.setCallerNo(caller);
                param.setIsLossCall(Constants.YES);
                param.setExcludeCallId(callId);

                List<CallLogDTO> list = callLogService.selectList(param);

                if(Objects.nonNull(list) && !list.isEmpty()){
                    for(CallLogDTO callLog : list){
                        CallLogDTO updateDTO = new CallLogDTO();

                        updateDTO.setCallId(callLog.getCallId());
                        updateDTO.setIsLossCall(WhetherEnum.NO.getCode());
                        updateDTO.setIsRingLossCall(WhetherEnum.NO.getCode());
                        updateDTO.setRingLossCallCode(0);
                        updateDTO.setRingTime(now);
                        updateDTO.setInSeatTime(now);
                        updateDTO.setIsIncludedCallLoss(WhetherEnum.NO.getCode());

                        callLogService.update2(updateDTO);
                    }
                }
                callLogService.update2(callLogDTO);
            }
        }
    }


    /**
     * 生成录音文件ID
     *
     * @return
     */
    private String getRecordId(){

        String dateStr = DateUtils.format(new Date(), DateConstant.PURE_DATE);
        String seqName = Constants.RECORD_LOG_SEQ_PREFIX + dateStr;
        String seqNo = orderSeqService.getSeq(seqName,6);
        String recordId = dateStr + seqNo;

        return recordId;
    }

    /**
     * 根据录音记录业务转换业务类型
     * @param recordBusinessCode
     * @return
     */
    private Integer getBusinessTypeCode(Integer recordBusinessCode){

        RecordBusinessEnum recordBusinessEnum = RecordBusinessEnum.getRecordBusinessEnum(recordBusinessCode);

        Integer businessType = null;
        if(Objects.nonNull(recordBusinessEnum)){
            switch (recordBusinessEnum){
                case CONSULT:{
                    businessType = BusinessTypeEnum.CONSULT.getCode();
                    break;
                }
                case QUERY:{
                    businessType = BusinessTypeEnum.QUERY.getCode();
                    break;
                }
                case COMPLAIN:{
                    businessType = BusinessTypeEnum.COMPLAIN.getCode();
                    break;
                }
                case SUGGESTION:{
                    businessType = BusinessTypeEnum.SUGGESTION.getCode();
                    break;
                }
                default:{
                    businessType = null;
                    break;
                }
            }
        }
        return businessType;
    }

    /**
     * 呼叫日志
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/call/vxml")
    public String call(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO){
        log.info("呼叫日志请求参数:{}",communicationDTO);

        String callId = null2String(communicationDTO.getSCallId());

        String unitCode = null2String(communicationDTO.getSUnitCode());

        String callerNo = null2String(communicationDTO.getSCallerNo());

        String calledNo = null2String(communicationDTO.getSCalledNo());

        String callType = null2String(communicationDTO.getSCallType());

        String operationTypeStr = null2String(communicationDTO.getSPlayType());

        String channelNo = "0";

        Integer operationType = string2Int(operationTypeStr);
        CallLogOperationTypeEnum callLogOperationTypeEnum = CallLogOperationTypeEnum.getCallLogOperationTypeEnum(operationType);

        CallLogDTO callLogDTO = new CallLogDTO();
        callLogDTO.setCallId(callId);
        callLogDTO.setUnitCode(UnitCodeUtil.getUnitCode(unitCode));

        if(Objects.nonNull(callLogOperationTypeEnum)){
            switch (callLogOperationTypeEnum){
                //进入IVR
                case IVR:{
                    callLogDTO.setCallerNo(callerNo);
                    callLogDTO.setCalledNo(calledNo);
                    callLogDTO.setCallType(string2Int(callType));
                    callLogDTO.setChannelNo(0);
                    callLogService.insert(callLogDTO);
                    break;
                }
                //排队
                case QUEUE:{
                    CallLogDTO callLog = callLogService.selectById(callId);
                    if(Objects.isNull(callLog) || Objects.isNull(callLog.getFirstInQueueTime())){
                        callLogDTO.setFirstInQueueTime(new Date());
                    }
                    callLogDTO.setInQueueTime(new Date());
                    callLogDTO.setOutTime(new Date());
                    callLogService.update(callLogDTO);
                    break;
                }
                //开始通话
                case CALL:{
                    callLogDTO.setInSeatTime(new Date());
                    callLogDTO.setOutTime(new Date());
                    callLogService.update(callLogDTO);
                    break;
                }
                //挂机
                case HANG_UP:{
                    callLogDTO.setOutTime(new Date());
                    callLogService.update(callLogDTO);
                    break;
                }
                //继续排队
                case QUEUE_CONTINUE:{

                    CallLogDTO callLog = callLogService.selectById(callId);
                    if(Objects.isNull(callLog) || Objects.isNull(callLog.getFirstInQueueTime())){
                        callLogDTO.setFirstInQueueTime(new Date());
                    }

                    if(Objects.isNull(callLog) || Objects.isNull(callLog.getInQueueTime())){
                        callLogDTO.setInQueueTime(new Date());
                    }
                    //排队次数
                    Integer queueTimes = 1;
                    if(Objects.nonNull(callLog) && Objects.nonNull(callLog.getQueueTimes())){
                        queueTimes =  callLog.getQueueTimes() + 1;
                    }
                    callLogDTO.setQueueTimes(queueTimes);
                    callLogDTO.setOutTime(new Date());
                    callLogService.update(callLogDTO);
                    break;
                }
            }
        }

        Map<String,String> result = new LinkedHashMap<>();
        result.put("sCalledNO", calledNo);
        result.put("sCallerNo", callerNo);
        result.put("sOperType", operationTypeStr);
        result.put("nCallType", callType);
        result.put("sChanelNo", channelNo);
        result.put("sCallid", callId);
        result.put("sUnitCode", unitCode);

        String vxml = VxmlUtil.createVxml(result);

        log.info("呼叫日志结果返回:{}",vxml);
        return vxml;
    }


    /**
     * 满意度调查
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/satisFaction/vxml")
    public String satisfaction(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO) {
        log.info("满意度调查请求参数:{}",communicationDTO);

        String callData = communicationDTO.getSCallData();
        String choose = communicationDTO.getSChoose();

        String vxml = VxmlUtil.createVxml();

        if(StringUtils.isBlank(callData)){
            log.info("callData为空，满意度调查结果返回:{}",vxml);
            return vxml;
        }

        String callDataArr[] = callData.trim().split("&");

        if(callDataArr.length >= 2){
            String callId = callDataArr[1];
            SatisfactionDTO satisfactionDTO = new SatisfactionDTO();
            satisfactionDTO.setCallId(callId);
            satisfactionDTO.setChoose(choose);

            satisfactionService.insert(satisfactionDTO);
        }
        log.info("满意度调查结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 值班人员
     *
     * @param communicationDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/db/vxml")
    public String db(@ModelAttribute("communicationDTO") CommunicationDTO communicationDTO) {

        log.info("db请求参数:{}",communicationDTO);

        String code = communicationDTO.getSCode();

        String vxml = VxmlUtil.createVxml();

        if(CommDBCode.GET_TEL_NUMBER.getCode().equals(code)){
            vxml = dutyUser(communicationDTO);
        }

        log.info("db请求结果返回:{}",vxml);
        return vxml;
    }

    /**
     * 值班人员
     * @param communicationDTO
     * @return
     */
    private String dutyUser(CommunicationDTO communicationDTO){

        String unitCode = UnitCodeUtil.getUnitCode(communicationDTO.getSUnitCode());
        String callId = communicationDTO.getSCallId();
        Integer isWorkTime = string2Int(communicationDTO.getSIsworktime());
        Integer getOrSet = string2Int(communicationDTO.getSGetOrSet());

        String vxml = VxmlUtil.createVxml();

        //非工作时间直接返回
        if(!Constants.YES.equals(isWorkTime)){
            log.info("非工作时间查询值班人员,结果返回:{}",vxml);
            return vxml;
        }

        GetOrSetEnum getOrSetEnum = GetOrSetEnum.getGenderEnum(getOrSet);
        if(Objects.nonNull(getOrSetEnum)){
            switch (getOrSetEnum){
                //获取号码
                case GET:{
                    DutyUserDTO dutyUserDTO = dutyUserService.getDutyUser(unitCode,callId,null);
                    log.info("工作时间查询值班人员:{}",dutyUserDTO);
                    if(Objects.nonNull(dutyUserDTO)){
                        Map<String,String> result = new LinkedHashMap<>();
                        result.put("rs", dutyUserDTO.getPhone());
                        vxml = VxmlUtil.createVxml(result);
                    }
                    break;
                }
                //重置号码
                case SET:{
                    log.info("重置值班人员:{}",callId);
                    DutyUserDTO dutyUserDTO = new DutyUserDTO();
                    dutyUserDTO.setCallId(callId);
                    dutyUserDTO.setIsLock(Constants.NO);
                    dutyUserService.updateByCallId(dutyUserDTO);
                    break;
                }
            }
        }
        return vxml;
    }
}
